package com.zle.services.code;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zle.common.util.DateUtil;
import com.zle.common.util.StringUtils;
import com.zle.dao.CodeMapper;
import com.zle.entity.Code;
import com.zle.entity.CodeExample;
import com.zle.entity.CodeExample.Criteria;

@Service
public class CodekService implements ICodeService{
	@Autowired
	private CodeMapper codeMapper;
	@Override
	public Map<String,Object> serviceInfo(String type){
		
		Map<String,Object> chartData = new HashMap<String,Object>();
		CodeExample example = new CodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andCodeTypeEqualTo(type);
		criteria.andVar1GreaterThan(DateUtil.getStringDate(DateUtil.getDayOfBegin(DateUtil.getCurrentSeconds())));
		List<Code> cpuDate = codeMapper.selectByExample(example);
		
		List<String> date = new ArrayList<String>();
		List<String> data = new ArrayList<String>();
		
		for(Code code : cpuDate){
			date.add(code.getVar1());
			data.add(code.getCodeValue());
		}
		chartData.put("date", date);
		chartData.put("data", data);
		return chartData;
	}
	@Override
	public void deleteHistorDates(String type,String start,String end){
		
		CodeExample example = new CodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andCodeTypeEqualTo(type);
		if(StringUtils.isNotEmpty(start)){
			
			criteria.andVar1GreaterThan(start);
		}
		if(StringUtils.isNotEmpty(end)){
			
			criteria.andVar1LessThan(end);
		}
		codeMapper.deleteByExample(example);
	}
}
